<template>
	<view class="content"  :style="'height:'+ screenHeight +'px !important;'">
		<view class="header">欢迎加入蚌壳找房</view>
        <view class="body">
            <view class="uni-form-item uni-column">
                <input type="tel" class="uni-input" name="" v-model="IDnumber" placeholder="请输入身份证号" />
            </view>
            <view class="uni-form-item uni-column">
                <input type="tel" class="uni-input" name="" v-model="username" placeholder="请输入真实姓名" />
            </view>
            <button type="photoButton" @click="uploadApplyPhoto" >点击上传身份证照片</button>
            <button class="add" @tap="addagent">申请加入</button>
        </view>
    </view>
</template>

<script>
import { mapState } from 'vuex';
	export default {
		data() {
			return {
				IDnumber: '',
                username: '',
                screenHeight: '',
			}
		},
		methods: {
			addagent() {
                var reg = /^[1-9]\d{5}(18|19|20|(3\d))\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
                if(reg.test(this.IDnumber) === false)
                {uni.showToast({
                    title: '身份证号不合法',
                    icon:"none",
                    duration: 2000
                    });
                }
                // console.log(this.IDnumber != /^[1-9]\d{5}(18|19|20|(3\d))\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/)
                else{
                    this.addAgentApply()
                    // console.log(this.IDnumber)
                    // console.log(this.username)
                    // console.log(this.applyPhoto[0])
                }
            },
            uploadApplyPhoto() {
                uni.chooseImage({
					count:1,
					success: (res) => {
						this.applyPhoto = res.tempFilePaths
					}
				})
            },
            addAgentApply(){
                this.$axios.post('addAgentApply', this.qs.stringify({
                    userId: this.user.userId,
                    applyIdnum: this.IDnumber,
                    applyUsername: this.username,
                    applyPhoto:this.applyPhoto[0]
                }))
                .then(res =>{
                    console.log(res)
                    console.log(this.applyPhoto[0])
                    uni.uploadFile({
                        url: 'http://192.168.43.133:8002/' + 'upload/applyPhoto',
                        filePath: this.applyPhoto[0],
                        name: 'applyPhoto',
                        formData: {
                            'applyId':  res.data.ApplyId
                        },
                        success: (uploadFileRes) => {
                            console.log(uploadFileRes);
                        },
                        fail: e => {
                            console.log(e)
                        }
                    })
                    uni.showToast({
                    title: '申请成功',
                    duration: 2000
                    });
                    uni.navigateBack();
                })
                .catch(err => {
                    console.log(err)
                })
            }
		},
        onLoad(){
            this.screenHeight = uni.getSystemInfoSync().windowHeight;
        },
        computed:{
            ...mapState(['user'])
        }
	}
</script>

<style>
.content{
    background-image: url('http://192.168.43.54:8002/images/background/invite.jpg');
    background-size: 100% 100%;
}
.header{
    font-size: 200%;
    text-align: center;
    padding: 600rpx 0rpx 30rpx;
}
.body{
    padding: 10rpx 80rpx;
}
.add{
    margin: 30rpx 0rpx;
    background-color: rgb(85, 85, 228);
    color: rgb(255, 255, 255);
    font-size: 120%;
}
</style>
